package com.billsharing.server;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class FriendBalance
 */
@WebServlet("/FriendBalance")
public class FriendBalance extends HttpServlet {
	private static final long serialVersionUID = 1L;
	int uid;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FriendBalance() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		response.setContentType("text/x-json;charset=UTF-8");
uid = Integer.parseInt(request.getParameter("uid"));
String friendEmail = request.getParameter("friendEmail");
friendEmail = friendEmail.replaceAll("\\s+","");		   
User user = new User(uid);
Map<String,Double> credit = user.getCreditInfo();
Map<String,Double> debit = user.getDebitInfo();
System.out.println(credit.get(friendEmail));
JSONObject json = new JSONObject();
if(credit.get(friendEmail) != 0.0)
{
	json.put("credit",credit.get(friendEmail));
}
else if(debit.get(friendEmail) != 0.0)
{
	json.put("debit", debit.get(friendEmail));
}
else
{
	json.put("false","false");
}
//String paymentRecevied = request.getParameter("recieved");
//Double amount  = Double.parseDouble(request.getParameter("amount"));
System.out.println(friendEmail);
out.print(json);
//User user = new User(uid);
//user.reportPayment(friendEmail, amount);


	}
	
	

}
